package com.banshuibao.bsb_android.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.adapter.NewSearchAdapter;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.MediaModel;
import com.banshuibao.bsb_android.model.NewSearchResultModel;
import com.banshuibao.bsb_android.model.SearchRequestModel;
import com.banshuibao.bsb_android.util.DialogUtils;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.XToast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class NewSearchActivity extends BaseActivity implements View.OnClickListener, NewSearchAdapter.OnItemClickListener {

    private RecyclerView recyclerView;

    private NewSearchAdapter adapter;

    private TextView search_bt;

    private EditText editText;
    private String title;
    private double price;
    private String id;
    private ImageView empty;
    private String type;
    private String img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_search);
        initView();
    }

    public void initView() {
        search_bt = findViewById(R.id.menu);
        search_bt.setOnClickListener(this);
        editText = findViewById(R.id.et);
        empty = findViewById(R.id.empty);
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new NewSearchAdapter(this);
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {
                    adapter.clearAdapter();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public void requestNet(String key) {
        showProgressDialog();
        adapter.clearAdapter();
        adapter.setKey(key);
        SearchRequestModel requestModel = new SearchRequestModel();
        requestModel.setContent(key);
        HttpUtil.doPost(Constants.Url.HOME_SEARCH2, requestModel, new HttpResponse(this, NewSearchResultModel.class) {
            @Override
            public void onResult(Object obj) {
                NewSearchResultModel newSearchResultModel = (NewSearchResultModel) obj;
                List<NewSearchResultModel.DataBean.BigVideosBean> bigVideos = newSearchResultModel.getData().getBigVideos();//大咖访谈视频集合
                List<NewSearchResultModel.DataBean.ExamVideosBean> examVideos = newSearchResultModel.getData().getExamVideos();//考前辅导视频集合
                List<NewSearchResultModel.DataBean.ExpertQuUsersBean> expertQuUsers = newSearchResultModel.getData().getExpertQuUsers();//专家答疑集合
                List<NewSearchResultModel.DataBean.PolicysBean> policys = newSearchResultModel.getData().getPolicys();//政策解读集合
                List<NewSearchResultModel.DataBean.TinyVideosBean> tinyVideos = newSearchResultModel.getData().getTinyVideos();//财税微课堂集合
                if (bigVideos.size() == 0 && examVideos.size() == 0 && expertQuUsers.size() == 0 && policys.size() == 0 && tinyVideos.size() == 0) {
                    recyclerView.setVisibility(View.GONE);
                    empty.setVisibility(View.VISIBLE);
                } else {
                    recyclerView.setVisibility(View.VISIBLE);
                    empty.setVisibility(View.GONE);
                    //顺序 专家、政策、课堂、考前、大咖
                    adapter.setAnswerData(expertQuUsers);
                    adapter.setCoachData(policys);
                    adapter.setLiveData(tinyVideos);
                    adapter.setTestData(examVideos);
                    adapter.setInterViewData(bigVideos);
                }
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                cancelProgressDialog();
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.menu:
                String key = editText.getText().toString().trim();
                if (!TextUtils.isEmpty(key)) {
                    requestNet(key);
                } else {
                    XToast.showToast(NewSearchActivity.this, "请输入搜索内容");
                }
                break;
        }
    }

    @Override
    public void VideoData(String title, double price, String id, String type, String img) {
        this.title = title;
        this.price = price;
        this.id = id;
        this.type = type;
        this.img = img;
        buyMedia(title, price, id, type, img);
    }


    void buyMedia(final String title, final double price, final String id, final String type, final String img) {
        MediaModel.BuyBody buyBody = new MediaModel.BuyBody();
        buyBody.setUuid(id);
        HttpUtil.doPost(Constants.Url.IS_BUY, buyBody, new HttpResponse(this) {
            @Override
            public void onResult(Object obj) {
                JSONObject jsonObject = null;
                JSONObject jsonObject1 = null;
                try {
                    jsonObject = new JSONObject((String) obj);
                    jsonObject1 = jsonObject.getJSONObject("data");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if (jsonObject1 != null) {
                    String type1 = jsonObject1.optString("type");
                    String vid = jsonObject1.optString("vid");
                    if ("0".equals(type1) && !TextUtils.isEmpty(vid)) {
                        if (TextUtils.equals(type, "1") || TextUtils.equals(type, "2"))
                            IntentManager.goMediaPlayActivity(context, vid, Constants.Url.VIDEO, id, img);
                        else if (TextUtils.equals(type, "3")) {
                            IntentManager.goMediaPlayActivity(context, vid, Constants.Url.INTER_VIDEO, id, img);
                        } else
                            IntentManager.goEventMediaPlayActivity(context, vid, id, Constants.Url.GAME_VIDEO, img);
                    } else if (TextUtils.equals("2", type1)) {
                        IntentManager.goCheckInActivity(context);
                    } else {
                        Dialog buyDialog = DialogUtils.payDialog(NewSearchActivity.this, "您可以支付" + price + "元购买单集或支付365元购买会员卡观看所有视频!", title, String.valueOf(price), id, 4);
                        if (!buyDialog.isShowing())
                            buyDialog.show();
//                        new AlertDialog.Builder(NewSearchActivity.this).
//                                setMessage("购买365会员可以免费观看所有视频,或者支付" + price + "元购买本集").
//                                setPositiveButton("购买", new DialogInterface.OnClickListener() {
//                                    @Override
//                                    public void onClick(DialogInterface dialogInterface, int i) {
//                                        //IntentManager.goPayResultActivity(getActivity(), data.getTitle(), String.valueOf(data.getPrice()), data.getId(), 4);
//                                        Intent intent = new Intent(NewSearchActivity.this, PayActivity.class);
//                                        intent.putExtra("flag", 4);
//                                        intent.putExtra("content", title);
//                                        intent.putExtra("price", String.valueOf(price));
//                                        intent.putExtra("uuid", id);
//                                        startActivityForResult(intent, 200);
//                                    }
//                                }).setNegativeButton("取消", null).create().show();
                    }
                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 200)
                buyMedia(title, price, id, type, img);
        }
    }
}
